#!/bin/sh
# PCP QA Test No. 1211
# Exercise basic pmseries loading and querying.
#
# Copyright (c) 2018-2019,2022 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"
path=""

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check
. ./common.keys

_check_series

_cleanup()
{
    [ -n "$options" ] && $keys_cli $options shutdown
    _restore_config $PCP_SYSCONF_DIR/pmseries
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
hostname=`hostname`
key_server_port=`_find_free_port`
options="-p $key_server_port"

trap "_cleanup; exit \$status" 0 1 2 3 15

_filter_source()
{
    sed \
	-e "s,$here,PATH,g" \
    #end
}

_sort_context()
{
    sed -e 's/    Context: //g' -e 's/, /\n/g' | \
    LC_COLLATE=POSIX sort | \
    $PCP_AWK_PROG 'BEGIN { printf "    Context: " } {
	if (count == 0) {printf("%s", $0)} else {printf(", %s", $0)};
	count++
    } END { print out }'
}

# real QA test starts here
_save_config $PCP_SYSCONF_DIR/pmseries
$sudo rm -f $PCP_SYSCONF_DIR/pmseries/*

echo "Start test key server ..."
$key_server --port $key_server_port --save "" > $tmp.keys 2>&1 &
_check_key_server_ping "$key_server_port"
_check_key_server $key_server_port
echo

_check_key_server_version $key_server_port

echo "Verify basic load from proc archive"
pmseries $options --load "{source.path: \"$here/archives/proc\"}" | _filter_source
echo

echo "Error handling - load from bad absolute path"
pmseries $options --load "{source.path: \"/no/such/archive\"}"
echo

echo "Error handling - load from bad relative path"
pmseries $options --load "{source.path: \"no/such/archive\"}"
echo

echo "Verify pminfo and pmseries source and series match"
echo "=> pminfo"
pminfo -smd --archive $here/archives/proc kernel.all.load
echo "=> pmseries"
loaded=`pmseries $options "kernel.all.load{source.name:\"$here/archives/proc\"}"`
series=fecd5a4b4c6e1273eaa001287a6dd57b7bbd19f7
second=d51624d12da45900bfee2fd73f1e23f3ccabb784
source=2cd6a38f9339f2dd1f0b4775bda89a9e7244def6
if [ "$loaded" != "$series" ]
then
    echo "Series identifier mismatch - loaded $loaded, expected $series"
    exit 1
fi
pmseries $options -smd $series
echo

echo "List all metric names ..."
pmseries $options -m | LC_COLLATE=POSIX sort
echo

echo "List some metric names ..."
pmseries $options -m -g 'hinv*' | LC_COLLATE=POSIX sort
echo

echo "List all label names ..."
pmseries $options -l | LC_COLLATE=POSIX sort
echo

echo "List some label names ..."
pmseries $options -l -g 'h*' | LC_COLLATE=POSIX sort
echo

echo "List all context names ..."
pmseries $options -S | _filter_source | LC_COLLATE=POSIX sort
echo

echo "List some context names ..."
pmseries $options -S -g 'bozo*' | _filter_source | LC_COLLATE=POSIX sort
echo

echo "List all instance names ..."
pmseries $options -i | LC_COLLATE=POSIX sort
echo

echo "List some instance names ..."
pmseries $options -i -g 'sda*' | LC_COLLATE=POSIX sort
echo

echo "Perform simple name-based query ..."
pmseries $options kernel.all.pswitch | LC_COLLATE=POSIX sort
echo

echo "Perform simple label-based query ..."
pmseries $options 'kernel.all.load{hostname:"bozo-laptop"}' | LC_COLLATE=POSIX sort
echo

echo "Perform 40-character non-SeriesID query ..."
pmseries $options 'kernel.all.load{hostname: "bozo-laptop"}' | LC_COLLATE=POSIX sort
echo

echo "Perform simple instance-based query ..."
pmseries $options 'kernel.all.load{instance.name:"1 minute"}'
echo

echo "Perform globbing name-based query (1) ..."
pmseries $options 'kernel.all.ps*' | LC_COLLATE=POSIX sort
echo

echo "Perform globbing name-based query (2) ..."
pmseries $options 'kernel*pswitch' | LC_COLLATE=POSIX sort
echo

echo "Perform globbing name-based query (3) ..."
pmseries $options 'kernel.all.l*d' | LC_COLLATE=POSIX sort
echo

echo "Perform globbing name-based query (4) ..."
pmseries $options 'disk.partitions.blk*' | LC_COLLATE=POSIX sort
echo

echo "Perform globbing instance-based query (1) ..."
pmseries $options 'kernel.all.load{instance.name:"1*"}'
echo

echo "Perform globbing instance-based query (2) ... no result"
pmseries $options 'kernel.all.load{instance.name:"1\*"}'
echo "(expect no result)"
echo

echo "Perform globbing instance-based query (3) ..."
pmseries $options 'kernel.all.load{instance.name:"*1*"}'
echo

echo "Perform regex instance-based query (1) ..."
pmseries $options 'kernel.all.load{instance.name=~"1.*"}'
echo

echo "Perform regex instance-based query (2) ... no result"
pmseries $options 'kernel.all.load{instance.name=~"1\*"}'
echo "(expect no result)"
echo

# kernel.all.load from proc archive (host:bozo-laptop)
echo "Descriptor lookup for a given SeriesID ..."
pmseries $options --desc $series
echo

echo "Full InDom only for a given SeriesID ..."
pmseries $options --fullindom $series
echo

echo "Full PMID only for a given SeriesID ..."
pmseries $options --fullpmid $series
echo

echo "All three lookups for a given SeriesID ..."
pmseries $options -dIM $series
echo

echo "SourceID lookup for a given SeriesID ..."
pmseries $options --sources $source | _filter_source | _sort_context
echo

echo "Instances lookup for a given SeriesID ..."
pmseries $options --instances $series
echo

echo "Labels lookup for a given SeriesID ..."
pmseries $options --labels $series
echo

echo "All lookups for a given SeriesID ..."
pmseries $options --all $series
echo

echo "Labels lookup for two given SeriesIDs ..."
pmseries $options --labels $second $series
echo

echo "Values fetch for a single-valued query ..."
pmseries $options -Z UTC 'kernel.all.pswitch[count: 30]'
echo

echo "Values fetch with a one-second interval ..."
pmseries $options -Z UTC 'kernel.all.pswitch[delta: "1sec"]'
echo

echo "Values fetch with a two-second interval ..."
pmseries $options -Z UTC 'kernel.all.pswitch[delta: "2sec"]'
echo

echo "Values fetch for a multi-valued query ..."
pmseries $options -Z UTC 'kernel.all.load[count: 30]'
echo

echo "Multi-series lookups from a multi-series query ..."
pmseries $options 'disk.partitions.blk*' | \
	LC_COLLATE=POSIX sort | \
	tee -a $tmp.series
pmseries $options -a `cat $tmp.series`

echo "Time windowed values from a multi-series query ..."
pmseries $options -ZUTC -v -w 'count: 2' `cat $tmp.series`

echo "Perform simple source-based query ..."
pmseries $options 'kernel.all.pswitch{source.name:"bozo-laptop"}'
echo

echo "Error handling - descriptor for bad series identifier"
pmseries $options -d no.such.identifier
echo

echo "Error handling - metric name for bad series identifier"
pmseries $options -m no.such.identifier
echo

echo "Error handling - instance names for bad series identifier"
pmseries $options -i no.such.identifier
echo

echo "Error handling - label names for bad series identifier"
pmseries $options -l no.such.identifier
echo

echo "Error handling - query with no matching instance name"
pmseries $options 'kernel.all.load{instance.name == "5 mi*"}'

# success, all done
status=0
exit
